
/*
 * ! Ext JS Library 3.3.0 Copyright(c) 2006-2010 Ext JS, Inc.
 * licensing@extjs.com http://www.extjs.com/license
 */

//
// This is the main layout definition.
//
var currentFormId;
var currentTaskId;
var contentPanel;
Ext.ux.TabCloseMenu = function() {
	var tabs, menu, ctxItem;
	this.init = function(tp) {
		tabs = tp;
		tabs.on('contextmenu', onContextMenu);
	};

	function onContextMenu(ts, item, e) {
		if (!menu) { // create context menu on first right click
			menu = new Ext.menu.Menu([ {
				id : tabs.id + '-close',
				text : '关闭页面',
				iconCls : 'btnno',
				handler : function() {
					if (tabs.items.items.length == 1) {
						ctxItem.hide();
					}
					//									tabs.hideTabStripItem(ctxItem);
					//									ctxItem.hide();
					tabs.remove(ctxItem);
				}
			}, {
				id : tabs.id + '-close-others',
				text : '关闭其他页面',
				iconCls : 'btnno',
				handler : function() {
					tabs.items.each(function(item) {
						if (item != ctxItem) {
							//										tabs.hideTabStripItem(item);
							//										item.hide();
							tabs.remove(item);
						}
					});
				}
			}, {
				id : tabs.id + '-close-all',
				text : '关闭所有页面',
				iconCls : 'btnno',
				handler : function() {
					tabs.items.each(function(item) {
						{

							//										tabs.hideTabStripItem(item);
							//										item.hide();

							if (tabs.items.items.length == 1) {
								item.hide();
							}
							tabs.remove(item);
						}
					});
				}
			} ]);
		}
		ctxItem = item;
		menu.showAt(e.getPoint());
	}
};

Ext.chart.Chart.CHART_URL = 'images/charts.swf';
Ext.onReady(function() {
	Ext.QuickTips.init();
	contentPanel = new Ext.TabPanel({
		id : 'content-panel',
		region : 'center', // this is what makes this panel into a region
		// within the containing layout
		margins : '2 5 5 0',
		activeItem : 0,
		border : false,
		items : [ start ],// ,
		plugins : new Ext.ux.TabCloseMenu(),
		autoDestroy : false,
		layoutOnTabChange : true,
		addTab : function(id) {
			var page = Ext.getCmp(id);
			if (page != null) {
				this.items.each(function(item) {
					if (item.id == id) {
						item.doLayout();
						contentPanel.setActiveTab(item);
						return;
					}
				});

				contentPanel.add(page).show();
				page.doLayout();
				contentPanel.setActiveTab(page);
			}
		},
		listeners : {
			'beforeremove' : function(tabPanel, panel) {
				if (tabPanel.items.items.length <= 1) {
					contentPanel.setActiveTab(null);
					return;
				}
				var active = tabPanel.getActiveTab();
				if (active.id == panel.id) {
					for ( var i = 0; i < tabPanel.items.items.length; i++) {
						var cp = tabPanel.items.items[i];
						if (cp.id == panel.id) {
							var p;
							if (i > 0) {
								p = tabPanel.items.items[i - 1];

							} else {
								p = tabPanel.items.items[1];
							}
							contentPanel.setActiveTab(p);
							break;
						}
					}
				}
			}
		//								'remove':function(tabPanel, panel){
		//									alert(1);
		//								}
		}
	});

	// alert(getQueryString('type'));
	// Assign the changeLayout function to be called on tree node click.
	treePanel.on('click', function(n) {
		var sn = this.selModel.selNode || {}; // selNode is null on
		// initial
		// selection
		if (n.leaf && n.id != sn.id) { // ignore clicks on folders and
			// currently selected node
			// Ext.getCmp('content-panel').layout.setActiveItem(n.parentNode.id
			// + '-panel');
			Ext.getCmp('content-panel').layout.setActiveItem(n.id + '-panel');
			contentInit(n.parentNode.id, n.id, n.text);

		}
	});
	// Ext.getCmp('domainModelXml').addListener('menu',menufunctino);
	var nodeName;
	var pnodeName;
	treePanel.on('contextmenu', function(node, e) {
		if (node.parentNode.id == 'domainModelXml') {
			if (node.isLeaf()) {
				e.preventDefault;
				node.select();
				nodeName = node.text;
				pnodeName = "domain";
				treeMenu.showAt(e.getXY());

			}
		}
		if (node.id == 'domainModelXml') {
			e.preventDefault;
			node.select();

			refreshMenu.showAt(e.getXY());
		}
		if (node.parentNode.id == 'domainModelXml') {
			if (node.isLeaf()) {
				e.preventDefault;
				node.select();
				nodeName = node.text;
				treeMenu.showAt(e.getXY());

			}
		}
		if (node.id == 'domainModelXml') {
			e.preventDefault;
			node.select();

			refreshMenu.showAt(e.getXY());
		}

		if (node.parentNode.id == 'epcModelXml') {
			if (node.isLeaf()) {
				e.preventDefault;
				node.select();
				nodeName = node.text;
				pnodeName = "epc";
				treeMenu1.showAt(e.getXY());

			}
		}
		if (node.id == 'epcModelXml') {
			e.preventDefault;
			node.select();
			refreshMenu1.showAt(e.getXY());
		}

		if (node.parentNode.id == 'functionModelXml') {
			if (node.isLeaf()) {
				e.preventDefault;
				node.select();
				nodeName = node.text;
				treeMenu2.showAt(e.getXY());

			}
		}
		if (node.id == 'functionModelXml') {
			e.preventDefault;
			node.select();
			refreshMenu2.showAt(e.getXY());
		}
		if (node.parentNode.id == 'departmentModelXml') {
			if (node.isLeaf()) {
				e.preventDefault;
				node.select();
				nodeName = node.text;
				treeMenu3.showAt(e.getXY());

			}
		}
		if (node.id == 'departmentModelXml') {
			e.preventDefault;
			node.select();
			refreshMenu3.showAt(e.getXY());
		}
	});

	function ExportHandler() {
		// Ext.MessageBox.alert(nodeName);
		// Ext.MessageBox.wait('正在处理',nodeName,'等待....'); //this
		// function is useful
		Ext.Ajax.request({
			method : 'POST',// 后台路径
			url : 'exportOripsAction',// 向后台传递参数
			params : {
				nodeName : nodeName,
				pnodeName : pnodeName
			},
			success : function(request) {

				Ext.MessageBox.alert('传输成功');
			}

		});
	}

	function refresh() {

		root.reload();
		var node = treePanel.getNodeById("model");
		node.expand(); // parent tree should expand first
		var node1 = node.getOwnerTree().getNodeById("domainModelXml");
		node1.expand();
		refreshMenu.hidden;

	}

	// This is the Details panel that contains the description for each
	// example
	// layout.

	// Finally, build the main layout once all the pieces are ready.
	// This is
	// also a good
	// example of putting together a full-screen BorderLayout within a
	// Viewport.
	new Ext.Viewport({
		layout : 'border',
		title : 'WareHouse4WS',
		items : [ {
			xtype : 'box',
			region : 'north',
			applyTo : 'header',
			height : 30
		}, {
			region : 'west', // 左边布局
			title : '导航栏',
			collapsible : true,
			split : true,
			width : 250,
			minSize : 175,
			maxSize : 400,
			layout : 'fit',
			margins : '0 0 0 0',
			items : new Ext.TabPanel({
				border : false,
				activeTab : 0,
				tabPosition : 'bottom',
				items : [ treePanel

				]
			})
		}, contentPanel ],
		renderTo : Ext.getBody()
	});
	// style change
	Ext.get('styleswitcher_select').on('change', function(e, select) {
		var name = select[select.selectedIndex].value;
		setActiveStyleSheet(name);
	});
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	Ext.get('styleswitcher_select').dom.value = title;

	//跳转到插件上传页
	wizard.show();
	wizard.nextButton.setDisabled(false);
	wizard.onNextClick();
	wizard.onNextClick();
	wizard.onNextClick();
});

// contentPanel填充方法选择
function contentInit(pid, cid, text) {
	//				var page = Ext.getCmp(cid + '-panel');
	//				if (page != null) {
	//					contentPanel.addTab(cid + '-panel');
	//					page.showPage();
	//				}

	if (cid.length >= 30) {

		contentPanel.addTab('xml-panel');

		showXmlContent(cid);
	} else {
		var page = Ext.getCmp(cid + '-panel');
		if (page != null) {
			contentPanel.addTab(cid + '-panel');
			page.showPage();
		}
	}
}
// ////////////////////////////////////////////////